using System;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Editor
{
    [CustomEditor(typeof(MapSettingComponent))]
    public class MapSettingComponentEditor : UnityEditor.Editor
    {
        private MapSettingComponent _MapDataComponent;
        

        public void OnEnable()
        {
            _MapDataComponent = (target as MapSettingComponent);
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            _MapDataComponent._MapSprite = (Sprite) EditorGUILayout.ObjectField("地图纹理", _MapDataComponent._MapSprite, typeof(Sprite), false);

            if (GUILayout.Button("显示/刷新预览地图"))
            {
                if (_MapDataComponent._MapSprite)
                {
                    if (!_MapDataComponent._PreviewSpriteRenderer)
                    {
                        var go = new GameObject("MapPreview");
                        SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
                        sr.transform.localEulerAngles = new Vector3(90, 0, 0);
                        sr.transform.parent = _MapDataComponent.transform;
                        sr.color = new Color(1, 1, 1, 0.5f);
                        sr.drawMode = SpriteDrawMode.Sliced;
                        _MapDataComponent._PreviewSpriteRenderer = sr;
                    }

                    _MapDataComponent._PreviewSpriteRenderer.sprite = _MapDataComponent._MapSprite;
                    _MapDataComponent._PreviewSpriteRenderer.size = new Vector2(_MapDataComponent._MapSprite.texture.width, _MapDataComponent._MapSprite.texture.height);
                }
            }

            // 设置预览地图transform
            if (_MapDataComponent._PreviewSpriteRenderer)
            {
                _MapDataComponent._PreviewSpriteRenderer.transform.position = _MapDataComponent._MapOffset;
                _MapDataComponent._PreviewSpriteRenderer.transform.localScale = new Vector3(_MapDataComponent._OriginMapScale,
                    _MapDataComponent._OriginMapScale, _MapDataComponent._OriginMapScale);
                // 把数据存下来
                _MapDataComponent.SetBounds(_MapDataComponent._PreviewSpriteRenderer.transform.position, _MapDataComponent._PreviewSpriteRenderer.bounds.extents*2);
            }

            if (GUILayout.Button("删除预览地图"))
            {
                if (_MapDataComponent._PreviewSpriteRenderer)
                {
                    GameObject.DestroyImmediate(_MapDataComponent._PreviewSpriteRenderer.gameObject);
                    _MapDataComponent._PreviewSpriteRenderer = null;
                }
            }
            
            if (GUILayout.Button("导出地图数据"))
            {
                string foldPath = Path.GetDirectoryName(_MapDataComponent._ExportPath);
                if (!Directory.Exists(foldPath))
                {
                    Directory.CreateDirectory(foldPath);
                    AssetDatabase.Refresh();
                }
                
                PrefabUtility.SaveAsPrefabAsset(_MapDataComponent.gameObject, _MapDataComponent._ExportPath);
                AssetDatabase.Refresh();

                // 处理预览
                GameObject prefab = GameObject.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>(_MapDataComponent._ExportPath), Vector3.zero, Quaternion.identity);
                // 移除所有子节点
                for (int i = 0; i < prefab.transform.childCount; ++i)
                {
                    GameObject.DestroyImmediate(prefab.transform.GetChild(i).gameObject);
                }
                // 移除Editor部分数据
                MapSettingComponent mapDataComponent = prefab.GetComponent<MapSettingComponent>();
                mapDataComponent._ExportPath = string.Empty;
                mapDataComponent._MapSprite = null;
                mapDataComponent._PreviewSpriteRenderer = null;
                // 保存
                PrefabUtility.SaveAsPrefabAsset(prefab, _MapDataComponent._ExportPath);
                GameObject.DestroyImmediate(prefab);
                AssetDatabase.Refresh();

                UnityEditor.EditorUtility.DisplayDialog("地图数据", "导出完成", "确定");
            }
        }
    }
}